package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Country;
import org.hgvc.webapp.ets.bo.State;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class StateBean extends AbstractBean {
	public static final String BEAN_NAME="stateBean";
	private MasterSetupService service;
	private State state;
	private List<State> stateList;
	private String countryId;
	private List<SelectItem> countryList;
	private Country country;
	private List<Country> countries;
	public static final String NAV_LIST_STATE="listState";
	public static final String NAV_ADD_STATE="addState";
	public static final String NAV_EDIT_STATE="editState";
	
	public void setCountry(Country country) {
		this.country = country;
	}
	public MasterSetupService getService() {
		return service;
	}
	public void setService(MasterSetupService service) {
		this.service = service;
	}
	public State getState() {
		return state;
	}
	public void setState(State state) {
		this.state = state;
	}
	
	public void setStateList(List<State> stateList) {
		this.stateList = stateList;
	}
	public String getCountryId() {
		return countryId;
	}
	public void setCountryId(String countryId) {
		this.countryId = countryId;
	}
	
	public void setCountryList(List<SelectItem> countryList) {
		this.countryList = countryList;
	}
	
	//action
	public List<SelectItem> getCountryList() {
		try{
			countries = service.getCountryList();
			countryList = new ArrayList<SelectItem>();
			for(Country country:countries){
				SelectItem item = new SelectItem(country.getCountryId(),country.getDesc());
				this.countryList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return countryList;
	}
	public Country getCountry() {
		if(countryId!=null&&countryId.length()!=0&&countryList!=null&&countryList.size()>0){
			for(Country country:countries){
				if(country.getCountryId().equalsIgnoreCase(countryId)){
					this.country = country;
					break;
				}
			}
		}else{
			country = new Country();
		}
		return country;
	}
	public List<State> getStateList() {
		try{
			if(countryId!=null&&countryId.length()!=0){
				stateList = service.getStateList(countryId);
			}else{
				stateList = new ArrayList<State>();
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return stateList;
	}
	
	public String navCreationAction(){
		state = new State();
		if(countryId!=null&&countryId.length()!=0){
			return NAV_ADD_STATE.concat("_"+getRoleName());
		}else{
			initErrorBean(new BaseException(SpringUtils.getMessage("error.select.country", new Object[]{})));
			return NAV_LIST_STATE.concat("_"+getRoleName());
		}
		
		
	}
	
	public String createAction(){
		try{
			state.setCreatedBy(getThisUser());
			state.setCreatedDate(new Date());
			state.setIsDeleted(CommonConstant.NOT_DELETED);
			state.setCountry(country);
			service.saveState(state,country);
			return NAV_LIST_STATE.concat("_"+getRoleName());
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_ADD_STATE.concat("_"+getRoleName());
	}
	
	public void editListener(ActionEvent actionEvent){
		state = null;
		UIParameter parameter = (UIParameter)actionEvent.getComponent().findComponent("stateId");
		String stateId = (String)parameter.getValue();
		for(State a:stateList){
			if(a.getStateId().equalsIgnoreCase(stateId)){
				state = a;
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT_STATE.concat("_"+getRoleName());
	}
	
	public String editAction(){
		try{
			state.setModifiedBy(getThisUser());
			state.setModifiedDate(new Date());
			service.updateState(state,state.getCountry());
			return NAV_LIST_STATE.concat("_"+getRoleName());
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		
		return NAV_EDIT_STATE.concat("_"+getRoleName());
	}
	
	public String deleteAction(){
		try{
			
			if(countryId==null||countryId.length()==0){
				throw new BaseException(SpringUtils.getMessage("error.select.country", new Object[]{}));
			}
			
			List<State> deleteList = new ArrayList<State>();
			for(State a:stateList){
				if(a.isMarkAsAction()){
					deleteList.add(a);
				}
			}
			service.deleteStates(deleteList);
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_LIST_STATE.concat("_"+getRoleName());
	}
	
	public String backToListAction(){
		return NAV_LIST_STATE.concat("_"+getRoleName());
	}
}
